package controller;

import dao.DrugDAOImpl;
import model.TDrug;
import utils.DispatcherUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "IndexServlet", value = "/index.do")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        DrugDAOImpl drugDAO = new DrugDAOImpl();
        List<TDrug> ar = null;
        String name= request.getParameter("name");

        if(name!=null) {
            if (name.equals("")) name = null;
        }

        try {
            ar = drugDAO.getTDrugsByTitle(name);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.setAttribute("ar",ar);

        /*
        用我们自己建的类将这个方法给简化掉
            RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
            rd.forward(request,response);*/

        DispatcherUtils.openweb("/index.jsp",request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
